//
//  Scenario.cpp
//  TrackingSim
//
//  Created by Garrett Manka on 11/25/11.
//  Copyright (c) 2011. All rights reserved.
//

#include <sim/Scenario.h>

namespace Sim
{
    Scenario::Scenario(double updateFrequency):m_update_frequency(updateFrequency)
    {
    }
    
    Scenario::~Scenario()
    {
        std::list< Player* >::iterator list_iter;
        
        for( list_iter = m_playerlist.begin() ; list_iter != m_playerlist.end() ; list_iter++ )
        {
            delete *list_iter;
        }
    }
    
    Scenario& Scenario::operator=(const Sim::Scenario& rhs)
    {
        //Handle self assignment
        if(this == &rhs)
        {
            return *this;
        }
        
        //Clear the list of the object being copied to so that additional elements are not simply appended
        m_playerlist.clear();
        
        std::list< Player* >::const_iterator iter = rhs.m_playerlist.begin();
        
        while( iter != rhs.m_playerlist.end() )
        {
            m_playerlist.push_back( new Player( *(*iter) ) );
            
            if( *iter == rhs.m_main_player )
            {
                m_main_player = m_playerlist.back();
            }
            
            ++iter;
        }
        
        m_update_frequency = rhs.m_update_frequency;
        
        return *this;
    }
    
    void Scenario::addPlayer(Sim::Player* player)
    {
        m_playerlist.push_back(player);
    }
    
    void Scenario::addPlayers(std::vector<Player *>& players)
    {
        for( unsigned int i = 0 ; i < players.size() ; ++i )
        {
            m_playerlist.push_back(players[i]);
        }
    }
    
    void Scenario::removePlayer(unsigned int playernum)
    {
        std::list< Player* >::iterator list_iter;
        
        for( list_iter = m_playerlist.begin() ; list_iter != m_playerlist.end() ; list_iter++ )
        {
            if( (*list_iter)->getPlayerNum() == playernum)
            {
                m_playerlist.erase(list_iter);
                break;
            }
        }
    }
    
    Player* Scenario::getPlayer(unsigned int playernum)
    {
        std::list< Player* >::iterator list_iter;
        
        for( list_iter = m_playerlist.begin() ; list_iter != m_playerlist.end() ; list_iter++ )
        {
            if( (*list_iter)->getPlayerNum() == playernum)
            {
                return *list_iter;
            }
        }
        
        return NULL;
    }
    
    Player* Scenario::getMainPlayer() const
    {
        return m_main_player;
    }
    
    void Scenario::setMainPlayer(Sim::Player *mainplayer)
    {
        m_main_player = mainplayer;
    }
    
    void Scenario::cycleMainPlayer()
    {
        static int num = 0;
        
        ++num;
    
        setMainPlayer(getPlayer( num % (int)m_playerlist.size() ) );
    }
    
    std::list< Player* >& Scenario::getPlayerList()
    {
        return m_playerlist;
    }
    
    double Scenario::getUpdateFrequency() const
    {
        return m_update_frequency;
    }
    
    double Scenario::getUpdatePeriod() const
    {
        return 1.0 / m_update_frequency;
    }
    
}
